/*
 *                 [[ Frozen-Bubble ]]
 *
 * Copyright (c) 2000-2007 Guillaume Cottenceau.
 * Flash sourcecode - Copyright (c) 2007 Mickael Foucaux.
 *
 * This code is distributed under the GNU General Public License 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * JavaScript version:
 *    Glenn Sanson <glenn.sanson at free.fr>
 *      (whole JavaScript sourcecode, including JIGA classes 
 *             http://glenn.sanson.free.fr/v2/?select=jiga )
 *
 * Flash version :
 *	  Mickael Foucaux <mickael.foucaux at gmail.com>
 * 	  http://code.google.com/p/frozenbubbleflash/
 * 
 */
 
class Game
{
	private var _main:Main;
	private var gameScreen:GameScreen;
	
	public var reset:Boolean;
	public var details:Number;
	public var colorBlind:Boolean;
	
	private var _mcTarget:MovieClip;
	public var mcBackground:MovieClip;
	public var mcPlayer:MovieClip;
	public var mcActiveBubble:MovieClip;
	public var mcStaticsBubbles:MovieClip;
	public var mcForeground:MovieClip;
	
	private var _aLevel:Array;
	
	public var level:Number;
	public var timer:Number;
	
	public var life:Number;
	
	private var _nRun:Number;
	
	public function Game(main:Main, mcTarget:MovieClip, nLevel:Number)
	{
		trace("Game on " + mcTarget);
		_mcTarget = mcTarget;
		initLevels();
		this._main = main;
		level = nLevel - 1;
		reset = getLevel(level) == "COMPLETE";
		life = 4;
	}
	
	public function startGame():Void
	{
		mcBackground = _mcTarget.createEmptyMovieClip("background", 10);
		mcPlayer = _mcTarget.createEmptyMovieClip("player", 20);
		mcActiveBubble = _mcTarget.createEmptyMovieClip("activeBubble", 30);
		mcStaticsBubbles = _mcTarget.createEmptyMovieClip("staticsBubbles", 40);
		mcForeground = _mcTarget.createEmptyMovieClip("mcForeground", 50);
		
		this.gameScreen = new GameScreen(this, getLevel(level), getLevel(level + 1), level, life);
		
		gameScreen.init();
				
		Key.addListener(gameScreen);
		_nRun = setInterval(gameScreen, "run", 5);
	}

	public function setBackground(sIdLink:String):Void
	{
		var sName:String = sIdLink + 10;
		mcBackground.attachMovie(sIdLink, sName, 10);
	}
	

	public function gameOver(bWin:Boolean):Void
	{
		clearInterval(_nRun);
		Key.removeListener(gameScreen);
		if (getLevel(level) == "COMPLETE")
		{
			level = 0;
		}
		
		life = gameScreen.life;

		if ((!bWin) && (life > 0))
		{
			startGame();
		}
		else
		{
			if (!bWin)
			{
				level = 0;
			}
			this._main.setState("gameOver", [bWin, level]);
		}
	}
	
	public function first_level():String
	{
		return getLevel(0);
	}
	
	public function getLevel(nLevel:Number):String
	{
		return _aLevel[nLevel];
	}
	
	public function setScreen(sScreenName:String):Void
	{
		gameOver(true);
	}
	
	private function initLevels():Void
	{
		_aLevel = [
			'77553344077553343344775503447755000000000000000000000000000000000000000000000000'
		,	'08888880002222200033330000003000000330000000600000066000000000000000000000000000'
		,	'00800800000828000002300000023200000360000004640000064000000040000000000000000000'
		,	'00011000000612000046270000540780085008500780005427000046020000060000000000000000'
		,	'00111100001222100121121000122210001111000008080000888800000000000000000000000000'
		,	'05557770050000070500007005342347042342300000000000000000000000000000000000000000'
		,	'05557770050000070500007005342347042342300034234000000000000000000000000000000000'
		,	'01100330006000400100070000400010050006000030004003000200004000500000000000000000'
		,	'40000004074357435000000503574357000700000000400000000000000000000000000000000000'
		,	'03020203023032022020303003202303030303030230320220203020033022030302020200000000'
		,	'08800660020000053200005403000004230000450200000582000056088000660000000000000000'
		,	'88000066026000853200005403000004260000850200000582000056086000860000000000000000'
		,	'00011000000612000046270000543730085833500788445427222446022000060000000000000000'
		,	'00100100004404400131131000440440001001000000000000000000000000000000000000000000'
		,	'00022000000333000044440000555550066666600000700000088000000010000000000000000000'
		,	'COMPLETE'
		];

/*		);
		INSERT INTO `fb_js_levels` VALUES (0, 14, );
		INSERT INTO `fb_js_levels` VALUES (0, 15, '00036000005400007806300000000450000360870054000078063000000004500000008700000000');
		INSERT INTO `fb_js_levels` VALUES (0, 16, '00066000000040000002000000008000000300000000500000060000000040000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 17, '00012000000138800001280000111100011122000111222001122200001118800088000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 18, '02000000020000000345876000000002000000200034587002000000020000000345876000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 19, '07000000060000003458763400000005000000800054367008000000070000006345876000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 20, '43211234043212345432234505432345654334560654345676544567076545678765567800000000');
		INSERT INTO `fb_js_levels` VALUES (0, 21, '00066000000040000003500000007000000350000030605021212121040403070000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 22, '00002000085460007002000000006458700020070854600000020007000064580000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 23, '00008470000484740068474000784780088472000484740067382000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 24, '60000006060777060650056000400040071001700040004000500500000777000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 25, '08100180080101088201102808231328874334780804340808844880000040000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 26, '04020807060808077010604000302060050405000304030000507000000060000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 27, '00002000000004007242325200000700000520000002040000032000000005000007200000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 28, '00065000000521000003400000250330042362500053015000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 29, '00002000000020000300206006002001070020500010206000661200000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 30, '00074000000437000037430000743740043743700374374374374374000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 31, '77777777050000000436875400600000008754360005000000043687000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 32, '20800703070207240508308003800050704613080200000202568620000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 33, '77700777000707000034430000406040006446000007270005300350000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 34, '00066000000600000457700604457606434776600445760604577006000600000006600000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 35, '20000002020333020234432007340437734004370734043744488444016131610000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 36, '00888000008338000866680008888880007070000070070007550755000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 37, '04404440048657642444044203232323244044420467568434440443022333220000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 38, '07600000042400000670000000064000007270000004600000004700000067600000740000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 39, '74856274062748567485627400000000000000000000000000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 40, '00000055000888550000005500200080022008000444080040344404003040440300000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 41, '00500000008500000085000000585000222222200232322033333333000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 42, '10000007072548611000000707254861100000070725486100000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 43, '44577544014575426245541201245421327441120145472672375113000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 44, '77000055051000471700005308000008550000670758867508757580001080800000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 45, '06000050006000500067750000030300117007220003030000877400008000400800004000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 46, '07000030028222420052250000242820000370000002620000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 47, '88888888080000088003163308000147800000510660000054730000013150000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 48, '00200200005006000800222007000080222205000006000000100000004000000200000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 49, '08800880070505076004400607000007080000800050005000400400000303000006600000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 50, '01100110085777546777377408577754011001100000000000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 51, '00000888000003880188808807888000700088880700000053335040055554440000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 52, '50080708087800850080080000111114001331750001112400011045000070670000002100000000');
		INSERT INTO `fb_js_levels` VALUES (0, 53, '06000060010010011113311101001001080400800004700000070000004700000400000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 54, '00076000000374000065820000733450004847000002430000056000000050000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 55, '88033077070070043002003006004003200300200600300370020080060060050000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 56, '00077000001555100007700000038300000770000016661000044000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 57, '00524000002002000052452000245005040045200024524000520000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 58, '07504360010000020346057001000002057036400100000206340570000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 59, '00077000000875000328524003222224033344400000600000034000000060000033440000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 60, '50600407030403055002100707034605500120070306040550400307070000050000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 61, '37166245020030015004700300001000000250000000300000074000000060000005200000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 62, '00006224016216446216216201621627000027620000062700002162000006210000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 63, '01840033001840030184003300184042018407560018408101840345001840670000081200000000');
		INSERT INTO `fb_js_levels` VALUES (0, 64, '00088880045600083000000408000005800045670800312370004567012000003450000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 65, '08000030022000440300005004400066050000700660002207000080000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 66, '05000050030020036001100606002007053886500000700000044000000080000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 67, '02003450030041505003420004054100500362000405615000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 68, '30022003030444030305503000881880000550000006860074375347060000020000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 69, '53468247020020024124354605005005060060060143156107007007080080080000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 70, '06502200060520201000001001750053054637470037006500000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 71, '00077000000665000027750000283640038326400324253804245380004667700000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 72, '00840000002870000486260008813515825767680288288200000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 73, '00200200006726700223322005821285048686400022222000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 74, '50006005077870565386337500052063063880850576505300050520011160000000111100000000');
		INSERT INTO `fb_js_levels` VALUES (0, 75, '20001100030012104001331005012221600155100700555080005500000012100001221000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 76, '00400280008500542001310006504000504722700020050208600040000400000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 77, '20002000030003000400440000505050060066000700828080007700000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 78, '30070362060505057004000405310006000704700006060000040536000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 79, '70005004014007010080204008058030634327040001546508700100054000531000007000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 80, '72362741000000051638273004000000787516370000000272517340010000000156487100000000');
		INSERT INTO `fb_js_levels` VALUES (0, 81, '00012000000181000023310000181810070880700527775206088060000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 82, '00067000000444000086480000407040300480020330402201367210000040000004800000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 83, '07000030003717100100000007000000044317110070000100071370003100000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 84, '18000000026000008360000007450000665500000446400023364000021187004466870000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 85, '00377300003221300343313003436383353638310353773103633383003677830033333000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 86, '00100100021121122886688204303034480770840800700855600655000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 87, '07400470070303073012210306108016060770600825052880500508031000130300003000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 88, '72000051038666847200005103688864720000510317771472000051000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 89, '60022006060505060350053008300038101551010830003803400430060404066007700600000000');
		INSERT INTO `fb_js_levels` VALUES (0, 90, '33000066060000036000000302026204636423630316031604800480000316000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 91, '17634528000002000002200000020000825436710000020000022000000200001763452800000000');
		INSERT INTO `fb_js_levels` VALUES (0, 92, '00200200003503500347643000760760000880000000800020088004030080030456752000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 93, '20033003024848530270073007084807053002400003720005445540000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 94, '00067000000040000002300000005000000680000000300076543286000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 95, '01020300005060700801030000704070020203000040601003050700004070800000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 96, '22334455066787667544332705768742000000000000000000000000000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 97, '85023058066030550608805002178713030640200220003372500537064000460000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 98, '26211262023606324723327405450545457667540134043134266243000000000000000000000000');
		INSERT INTO `fb_js_levels` VALUES (0, 99, '41386876070203020751456000602050084676400203050375577662000000000000000000000000');*/
	}
}
